<template>
  <el-dialog
    v-model="dialogFormVisible"
    custom-class="mydialog"
    :title="title"
    @close="close"
  >
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item>报表以申报日期范围为准，不限定上课时间.</el-form-item>
      <el-form-item label="报表名称" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="申报开始(包含)" prop="createtimeFrom">
        <el-date-picker
          v-model="form.createtimeFrom"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="申报截止(不含)" prop="createtimeTo">
        <el-date-picker
          v-model="form.createtimeTo"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { createReport } from '@/api/dksb'
  import { useUserStore } from '@/store/modules/user'
  import dayjs from 'dayjs'

  export default defineComponent({
    name: 'ReportEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      const userStore = useUserStore()
      const { corpId, userId } = userStore

      const state = reactive({
        formRef: null,
        form: {
          corpId,
          userId,
          title: `${dayjs()
            .subtract(1, 'month')
            .format('YYYY年MM月')}代课明细报表`,
          createtimeFrom: dayjs().subtract(1, 'month').format('YYYY-MM-14'),
          createtimeTo: dayjs().format('YYYY-MM-14'),
          status: 2,
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入名称' }],
          createtimeFrom: [
            { required: true, trigger: 'blur', message: '请输入开始日期' },
          ],
          createtimeTo: [
            { required: true, trigger: 'blur', message: '请输入结束日期' },
          ],
        },
        title: '创建报表',
        dialogFormVisible: false,
      })

      const showEdit = () => {
        state.dialogFormVisible = true
      }
      const close = () => {
        state.dialogFormVisible = false
      }
      const fetchData = async () => {}
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await createReport(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data', true)
            close()
          }
        })
      }
      onMounted(() => {
        fetchData()
      })
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>

<style lang="scss">
  .mydialog {
    width: 100%;
    max-width: 500px;
  }
</style>
